﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using bitzhuwei.Compiler;

namespace bitzhuwei.ExpFormat {
    public class PostItemList : IVnNode {
        private readonly List<IVnNode> list = new();

        public PostItemList(IVnNode item) {
            this.list.Add(item); item.Parent = this;
            this._scope = new TokenRange(item);
        }

        public void Add(IVnNode item) {
            this.list.Add(item); item.Parent = this;
            this._scope.end = item.Scope.end;
        }
        public IInherit? Parent { get; set; }

        private readonly TokenRange _scope;
        public TokenRange Scope => _scope;

        public void FullFormat(BlankConfig preConfig, TextWriter writer, FormatContext context) {
            // '+'或'-'与其左右两边的Token间隔1个空格
            var config = new BlankConfig(inlineBlank: 1, forceNewline: false);
            for (int i = 0; i < list.Count; i++) {
                if (i == 0) {
                    context.PrintBlanksAnd(list[i], preConfig, writer);
                }
                else {
                    context.PrintCommentsBetween(list[i - 1], list[i], config, writer);
                    context.PrintBlanksAnd(list[i], config, writer);
                }
            }
        }

        public IEnumerable<IInherit> ReversedChildren() {
            var count = this.list.Count;
            for (var i = count - 1; i >= 0; i--) {
                yield return this.list[i];
            }
        }
        public void PrintSelf(TextWriter writer, IReadOnlyList<string>? stArray) {
            writer.Write($"List T[{_scope}]");
        }
        public override string ToString() {
            return $"List T[{_scope}]";
        }

    }
}
